package com.intel.two.controller.building;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.intel.two.bo.building.ResidentBo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.common.R;
import com.intel.two.listener.building.WebResidentBoListener;
import com.intel.two.pojo.building.Resident;
import com.intel.two.service.building.ResidentService;
import com.intel.two.utils.OssUtil;
import com.intel.two.vo.building.BarVo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/resident")
public class ResidentController {
    @Autowired
    private ResidentService residentService;

    @RequestMapping("/residentList")
    public LayuiPageVo<Resident> employees(@RequestParam Map<String,Object> params){
        System.out.println("接收的参数"+params);

        if (params.isEmpty()) {
            params.put("page",1);
            params.put("limit",5);
        }

        return  residentService.residentPageData(params);
    }

    @RequestMapping("/uploadPic")
    public R test(MultipartFile file) throws Exception {
        System.out.println("上传照片");
        //得到文件名
        String originalFilename = file.getOriginalFilename();

        String fileUrl = OssUtil.updateLoad(file.getInputStream(), originalFilename);
        System.out.println(fileUrl);
        return R.ok().data("url",fileUrl);
    }

    @RequestMapping("/addResident")
    public R addResident(@RequestBody Resident resident){
        System.out.println("添加用户："+resident);
        int result = residentService.addResident(resident);
        System.out.println("添加结果："+result);
        if (result ==1) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    @RequestMapping("/delete")
    public R deleteResident(Integer id){
        System.out.println("删除Id："+id);
        int result = residentService.deleteResident(id);
        System.out.println("删除结果："+result);
        if (result>0) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    @GetMapping("/deleteResidents")
    public R deleteResidents(@RequestParam Map<String, Object> map) {
        System.out.println(map);
        int result = residentService.deleteResidents(map);
        return result >0 ? R.ok() : R.error();
    }

    @RequestMapping("/updateResident")
    public R updateResident(Resident resident){
        System.out.println("更新用户："+resident);
        int result = residentService.updateResident(resident);
        System.out.println("更新结果："+result);
        if (result ==1) {
            return R.ok();
        }else {
            return R.error();
        }
    }
    //柱状图信息
    @RequestMapping("echartBar")
    public R echartBar(){
        List<BarVo> barData= residentService.BarData();
        System.out.println(barData);

        return R.ok().data("barData",barData);
    }

    @RequestMapping("/export")
    public void export(Resident resident, HttpServletResponse response) throws IOException {
        System.out.println("输出excel文件");
        //下载 excel文件 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //防止中文亂碼
        String filename = URLEncoder.encode("居民信息导出","utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        // 获取输出流
        ServletOutputStream outputStream = response.getOutputStream();

        // easyexcel 写数据到 输出流
        ExcelWriterBuilder write = EasyExcel.write(outputStream, Resident.class);
        ExcelWriterSheetBuilder sheet = write.sheet();

        //列表数据
        List<ResidentBo> employeeExcelData =residentService.ResidentExcelData(resident);
        sheet.doWrite(employeeExcelData);
    }

    @Autowired
    private WebResidentBoListener webResidentBoListener;
    @RequestMapping("/import")
    public R importExcel(MultipartFile file) throws IOException {
        System.out.println("导入excel添加resident");
        ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), ResidentBo.class,webResidentBoListener );
        //得到 读取的sheet页对象
        ExcelReaderSheetBuilder sheet = read.sheet();
        //读取sheet
        sheet.doRead();

        return R.ok();
    }

    @RequestMapping("/templateDownload")
    public void templateDownload (HttpServletRequest request, HttpServletResponse response) throws IOException {
        //下载 excel文件 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        String realPath = request.getServletContext().getRealPath("/excel/居民列表模板.xlsx");
        //得到文件对象
        File file = new File(realPath);
        //文件对象转为字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);

        //防止中文亂碼
        String filename = URLEncoder.encode("居民列表模板","utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        ServletOutputStream outputStream = response.getOutputStream();

        outputStream.write(bytes);

        outputStream.close();

    }
}
